package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.Goods;
import com.xjh.t.domain.Saler;
import com.xjh.t.domain.User;
import com.xjh.t.service.IGoodsService;
import com.xjh.t.service.ISalerService;
import com.xjh.t.vo.GoodsVo;
import com.xjh.t.vo.SalerVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2025-11-09
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ISalerService salerService;

    @RequestMapping("loadAllGoods")
    public DataGridView loadAllGoods(GoodsVo vo) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vo.getGoodsName()), "goods_name", vo.getGoodsName());
        queryWrapper.eq(vo.getGoodsType() != null, "goods_type", vo.getGoodsType());
        queryWrapper.eq(vo.getHot() != null, "hot", vo.getHot());

        if(StringUtils.isNotBlank(vo.getBelongSalerName()))
        {
            QueryWrapper<Saler> queryWrapperSaler = new QueryWrapper<>();
            queryWrapperSaler.like(StringUtils.isNotBlank(vo.getBelongSalerName()), "saler_name", vo.getBelongSalerName());

            List<Saler> salerList = salerService.list(queryWrapperSaler);
            if(salerList.size() > 0)
            {
                List<Integer> salerIds = new ArrayList<>();
                for(Saler saler:salerList)
                    salerIds.add(saler.getId());
                queryWrapper.in("belong_saler",salerIds);
            }

        }
        //queryWrapper.like(StringUtils.isNotBlank(vo.getBelongSalerName()), "belong", vo.getMobilePhone());
        IPage<Goods> page = new Page<>(vo.getPage(), vo.getLimit());

        List<GoodsVo> goodsVos = new ArrayList<>();
        this.goodsService.page(page, queryWrapper);
        List<Goods>  goodsList = page.getRecords();

        List<Saler> salerList = this.salerService.list();
        Map<Integer, String> salermap = salerList.stream().collect(Collectors.toMap(Saler::getId, Saler::getSalerName));

        for(Goods goods:goodsList)
        {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(goods,goodsVo);
            goodsVo.setBelongSalerName(salermap.get(Integer.valueOf(goods.getBelongSaler())));
            goodsVos.add(goodsVo);
        }
        return new DataGridView(page.getTotal(), goodsVos);

    }

    @RequestMapping("addGoods")
    public ResultObj addGoods(Goods vo) {

        goodsService.save(vo);
        return ResultObj.ADD_SUCESS;

    }

    @RequestMapping("updateGoods")
    public ResultObj updateSaler(Goods vo) {

        goodsService.updateById(vo);
        return ResultObj.UPDATE_SUCESS;

    }

    @RequestMapping("deletetGoods")
    public ResultObj deletetGoods(int id) {

        goodsService.removeById(id);
        return ResultObj.DELETE_SUCESS;

    }

}

